<?php
	
	function elggchat_init() {
		if(isloggedin()){
			// Extend system CSS with our own styles
			extend_view('css','elggchat/css');
			
			extend_view('profile/menu/actions','elggchat/profile_menu_actions');
			extend_view('page_elements/header','elggchat/session_monitor');
		}
    }
	
	function elggchat_pagesetup()	{
		
	}
	
	// function for checking if a user is a member of a session
	function is_member($userid, $members){
		$result = false;

		foreach($members as $member){
			if($member->guid == $userid){
				$result = true;
			}
		}
		return $result;
	}
	
	// Cron Actions
	function elggchat_session_cleanup($hook, $entity_type, $returnvalue, $params){
		
		$maxsessions = get_entities("object", "elggchat_session",0,"",10,0,true);
		$chatsessions = get_entities("object", "elggchat_session",0,"",$maxsessions,0);
		$monitorUpdateInterval = get_plugin_setting("monitorUpdateInterval","elggchat");
		if(!$monitorUpdateInterval) $monitorUpdateInterval = 60;
		$maxInactive = $monitorUpdateInterval * 2;
		
		foreach($chatsessions as $session){
			if($session->time_updated < (time() - $maxInactive)){
				$session->delete();
			}
		}
	}

	register_elgg_event_handler('pagesetup','system','elggchat_pagesetup');
	register_elgg_event_handler('init','system','elggchat_init');
	
	// actions
	register_action("elggchat/create",false,$CONFIG->pluginspath . "elggchat/actions/create.php");
	register_action("elggchat/post_message",false,$CONFIG->pluginspath . "elggchat/actions/post_message.php");
	register_action("elggchat/poll",false,$CONFIG->pluginspath . "elggchat/actions/poll.php");
	register_action("elggchat/session_check",false,$CONFIG->pluginspath . "elggchat/actions/session_check.php");
	register_action("elggchat/session_info",false,$CONFIG->pluginspath . "elggchat/actions/session_info.php");
	register_action("elggchat/invite_friends",false,$CONFIG->pluginspath . "elggchat/actions/invite_friends.php");
	register_action("elggchat/invite",false,$CONFIG->pluginspath . "elggchat/actions/invite.php");
	register_action("elggchat/leave",false,$CONFIG->pluginspath . "elggchat/actions/leave.php");
	register_action("elggchat/get_smiley",false,$CONFIG->pluginspath . "elggchat/actions/get_smiley.php");
	register_action("elggchat/friends_picker",false,$CONFIG->pluginspath . "elggchat/actions/friends_picker.php");
	
	// Register Cron Jobs
	register_plugin_hook('cron', 'minute', 'elggchat_session_cleanup');
	
?>